//
//  CXShopCategoryView.m
//  Meituan_OC
//
//  Created by Tony on 16/9/26.
//  Copyright © 2016年 Tony. All rights reserved.
//

#import "CXShopCategoryView.h"


@interface CXShopCategoryView ()

@property (nonatomic, strong) UIView * vLine;

@property (nonatomic, strong) NSArray<UIButton *> * buttonArr;

@end

@implementation CXShopCategoryView




- (void) btnClick:(UIButton *)button {
    int tag = (int)(button.tag / 10 - 1);
//    NSLog(@"btnClick tag:%d", tag);
    
    // 如果当前点击的按钮与上一次点击的按钮是同一个，那么直接返回。
    if (self.selectedIndex == button.tag / 10 - 1) {
        return;
    }
    
    // 改变按钮选中状态并记录新按钮
    [self selectButtonWithTag:button.tag / 10 - 1];
    
     //方案一：update参照第一个button
    // 此处改变vLine的位置都要以第一个button的位置为参考
    [_vLine mas_updateConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(_buttonArr[0]).mas_offset(_buttonArr[0].bounds.size.width * tag);
    }];

//    // 方案二：remake参照传进来的button
//    [_vLine mas_remakeConstraints:^(MASConstraintMaker *make) {
//        make.width.bottom.equalTo(button);
//        make.height.mas_equalTo(4);
//        make.centerX.mas_equalTo(button);
//    }];
    
    [UIView animateWithDuration:0.5 animations:^{
        [self layoutIfNeeded];
    }];
    
    [self sendActionsForControlEvents:UIControlEventValueChanged]; // 发送值改变的消息通知
}

/// 改变按钮选中状态并记录新按钮
- (void) selectButtonWithTag:(NSInteger)tag {
    self.buttonArr[_selectedIndex].selected = NO;
    self.selectedIndex = tag;
    self.buttonArr[_selectedIndex].selected = YES;
}

#pragma markl - setupUI
-(void) setupUI {
    //
    self.backgroundColor = [UIColor whiteColor];
    
    // 创建三个按钮
    UIColor * normalColor = [UIColor cz_colorWithHex:0x555555];
    UIColor * selecedColor = [UIColor cz_colorWithHex:0xFF0000];
    
    NSArray * titleArr = @[@"点菜", @"评价", @"商家"];
    NSMutableArray<UIButton *> * buttons = [NSMutableArray array];
    
    for (NSString * title in titleArr) {
        UIButton * button = [UIButton cz_textButton:title fontSize:14 normalColor:normalColor selectedColor:selecedColor];
        [self addSubview:button];
        [buttons addObject:button];
    }
    
    // 给三个按钮自动布局
    [buttons[0] mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.leading.bottom.mas_equalTo(self);
    }];
    
    [buttons[1] mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(buttons[0].mas_trailing);
        make.size.top.equalTo(buttons[0]);
    }];
    
    [buttons[2] mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(buttons[1].mas_trailing);
        make.size.top.equalTo(buttons[0]);
        make.trailing.equalTo(self);
    }];
    
    _buttonArr = buttons;
    
    // 默认选中第0个按钮
    [self selectButtonWithTag:0];
    
    // 标签下的标示线
    [self.vLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.centerX.equalTo(_buttonArr[0]);
        make.bottom.equalTo(_buttonArr[0]);
        make.height.mas_equalTo(4);
    }];
    
    // 添加按钮点击事件
    int tag = 0;
    for (UIButton * btn in buttons) {
        [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
        btn.tag = ++tag * 10;
    }
}


#pragma mark - setter & getter

-(void)setOffSetX:(CGFloat)offSetX {
    _offSetX = offSetX;
    
    // 更改小标识线的布局 centerX
    [_vLine mas_updateConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(_buttonArr[0]).mas_offset(offSetX);
    }];
    
    NSInteger pageIndex = (NSInteger)(offSetX / _buttonArr[0].bounds.size.width + 0.5); // +0.5, 四舍五入！！！
    
    [self selectButtonWithTag:pageIndex];
}

#pragma mark - lazy.

-(UIView *)vLine {
    if (!_vLine) {
        _vLine = [[UIView alloc] init];
        _vLine.backgroundColor = [UIColor magentaColor];
        [self addSubview:_vLine];
    }
    return _vLine;
}

@end
